﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ServiceBus.Service;

namespace Test
{
    public partial class FmMonitor : Form
    {
        public FmMonitor()
        {
            InitializeComponent();
        }

        void Inventory_InventoryAddedEvent(object sender, InventoryArgs e)
        {
            throw new NotImplementedException();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           // this.InitializeComponent();
            //ServiceController.OpenFunctionService<ServiceAdapter>();
            //ServiceController.OpenMessageService();
            Inventory.InventoryAddedEvent += new EventHandler<InventoryArgs>(Inventory_InventoryChangedEvent);
            Inventory.InventoryChangedEvent += new EventHandler<InventoryArgs>(Inventory_InventoryChangedEvent);
            Inventory.OutofStockEvent += new EventHandler<InventoryArgs>(Inventory_OutofStockEvent);

            ServiceController.AddKnowType(typeof(Inventory));
            ServiceController.AddKnowType(typeof(Inventory[]));

            ServiceController.ServiceOpenedEvent += new EventHandler(ServiceController_ServiceOpenedEvent);
            ServiceController.ServiceFaultEvent += new EventHandler(ServiceController_ServiceFaultEvent);

            this.DisplayInventory();
        }

        void Inventory_OutofStockEvent(object sender, InventoryArgs e)
        {
            this.txtInfo.AppendText(e.Operator + " change "+e.ChangedNumber+" inventory fail. Reason: Out of Stock");
        }

        void Inventory_InventoryChangedEvent(object sender, InventoryArgs e)
        {
            this.DisplayInventory();
            this.txtInfo.AppendText(e.Operator + " change " + e.ChangedNumber + " iventory sucess");
        }

        void ServiceController_ServiceFaultEvent(object sender, EventArgs e)
        {
           this.txtInfo.AppendText(sender.ToString()+ " "+DateTime.Now.ToString()+"\r\n");
        }

        void ServiceController_ServiceOpenedEvent(object sender, EventArgs e)
        {
            this.txtInfo.AppendText(sender.ToString() + " " + DateTime.Now.ToString() + "\r\n");
        }

        private void DisplayInventory()
        {
            this.dataGridView1.DataSource = null;
            this.dataGridView1.DataSource = Inventory.GetInventoryList();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ServiceController.OpenFunctionService<ServiceAdapter>(int.Parse(this.txtPort.Text), this.txtName.Text);
            ServiceController.OpenMessageService(int.Parse(this.txtPort.Text), this.txtName.Text);
        }
    }
}
